package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.Area;
import br3akr.DeadlyDinnerParty.Server.World.Chest;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;
import br3akr.DeadlyDinnerParty.Server.World.Item;

public class PutAction extends Action {
	boolean actionComplete;
    private ActionResponse actionResponse;
    public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		tempKeywords.add("Put");
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public PutAction() {
        super("Put", false);
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (action == null) {
			throw new DeadlyDinnerPartyException ("No action found.");
		}
		if (session == null) {
			throw new DeadlyDinnerPartyException ("No session found.");
		}
		
    	StringTokenizer tokenizedAction = new StringTokenizer(action);
    	String keyword = tokenizedAction.nextToken();
    	String param1 = tokenizedAction.nextToken();
    	
		GameCharacter actor = session.getCharacter();
		
		if (actor == null) {
			throw new DeadlyDinnerPartyException ("No character found for your session.");
		}
    	
    	actionResponse = new ActionResponse();
    	actionResponse.setActingCharacter(session.getCharacter());
    	Area location = session.getCharacter().getLocation();
    	Chest chest = null;
    	
    	Set<GameCharacter> localOnlookers = location.getAreaOnlookers(session.getCharacter(), null);
    	actionResponse.addAffectedArea(location);
    	Item it = null;
    	
		switch (keyword.toLowerCase()) {
			case "put":
		    	if (param1 == null) {
		    		throw new DeadlyDinnerPartyException("Where do you want to put something?");
		    	}
		    	chest = location.getChest(param1);
		    	
		    	if (chest == null) {
		    		throw new DeadlyDinnerPartyException("There is no chest called " + param1 + ".");
		    	}
		    	
		    	if (chest.isLocked()) {
		    		throw new DeadlyDinnerPartyException(chest.getName() + " is locked.");
		    	}
		    	
		    	it = actor.getInventoryItem(action.substring(action.indexOf(param1) + param1.length()).trim());
		    	
		    	if (it == null) {
		    		throw new DeadlyDinnerPartyException("You're not carrying antying called " + action.substring(action.indexOf(param1) + param1.length()).trim());
		    	}
		    	
				actor.removeItem(it);
				chest.addItem(it);
				actionResponse.addOnlookers(localOnlookers, actor.getName() + " puts something in the chest called " + chest.getName() + ".");
				actionResponse.setActingCharacterDescription("You put " + it.getName() + " in the chest called " + chest.getName() + ".");
				beginRecovery(session.getCharacter(), 3000);
				break;
			default:
				break;
		}
        
    	actionComplete = true;
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }
}